import pymongo
import json
import requests
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["anjiu"]


def fcgetssq(start,stop):
    header = {
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "Cookie": "UniqueID=ynFQorypr2rMkr801625193961691; Sites=_21; _ga=GA1.3.646488792.1625149140; _gid=GA1.3.1288865627.1625149140; 21_vq=25",
        "Host": "www.cwl.gov.cn",
        "Referer": "http://www.cwl.gov.cn/kjxx/ssq/kjgg/",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest"
    }
    for i in range(start,stop+1):
        m = int(str(i)  + "000")
        while True:
            pram = {"name":"ssq","issueStart":str(m),"issueEnd":str(m + 100),"pageNo":1}
            print(pram)
            st=requests.get("http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice",params=pram,headers=header)
            try:
                st_json = json.loads(st.text)
                if st_json["message"] == "查询成功":
                    for i in st_json["result"]:
                        print(i["code"])
                        dict_ = {}
                        dict_["name"] = i["name"] 
                        dict_["code"] = i["code"] 
                        dict_["date"] = i["date"] 
                        dict_["red"] = i["red"] 
                        dict_["blue"] = i["blue"] 
                        dict_["sales"] = i["sales"] 
                        dict_["poolmoney"] = i["poolmoney"] 
                        dict_["content"] = i["content"] 
                        dict_["prizegrades"] = i["prizegrades"] 
                        mydb["fcssq"].insert_one(dict_)
                    if len(st_json["result"]) == 100:
                        m += 100
                    else:
                        break
                else:
                    print(st_json["message"])
                    time.sleep(1)
            except Exception as e:
                print(e)
                continue


def tcgetdlt(start,stop):
    header = {
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "Host": "webapi.sporttery.cn",
        "Origin": "https://static.sporttery.cn",
        "Referer": "https://static.sporttery.cn/",
        "sec-ch-ua": "' Not A;Brand';v='99', 'Chromium';v='90', 'Google Chrome';v='90'",
        "sec-ch-ua-mobile": "?0",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-site",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
    }
    for i in range(start,stop+1):
        s = int(str(i) + "000")
        while True:
            m = str(s + 100)
            s_t = str(s)[2:]
            m_t = m[2:]
            url = f"https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=85&provinceId=0&pageSize=100&isVerify=1&pageNo=1&startTerm={s_t}&endTerm={m_t}"
            st=requests.get(url,headers=header)
            try:
                st_json = json.loads(st.text)
                if st_json["errorMessage"] == "处理成功":
                    for i in st_json["value"]["list"]:
                        dict_ = {}
                        print(i["lotteryDrawNum"])
                        dict_["lotteryDrawNum"] = i["lotteryDrawNum"]
                        dict_["lotteryDrawResult"] = i["lotteryDrawResult"]
                        dict_["lotteryDrawTime"] = i["lotteryDrawTime"]
                        dict_["lotteryGameName"] = i["lotteryGameName"]
                        dict_["prizeLevelList"] = [{"prizeLevel":x["prizeLevel"],"stakeAmount":x["stakeAmount"],"stakeCount":x["stakeCount"],"totalPrizeamount":x["totalPrizeamount"]} for x in i["prizeLevelList"]]
                        mydb["tcdlt"].insert_one(dict_)
                    if len(st_json["value"]["list"]) == 100:
                        s += 100
                    else:
                        break
                else:
                    print(st_json["errorMessage"])
                    time.sleep(1)
            except Exception as e:
                print(e)
                continue

tcgetdlt(2007,2021)